//
//  NSString+GX_SHA_1.m
//  24-加密
//
//  Created by 曹曹 on 16/8/16.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "NSString+GX_SHA_1.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSString (GX_SHA_1)

- (NSString *)SHA_1String {
    // 1.将字符串转成C字符串
    const char *cString = [self UTF8String];
    // 2.求长度
    CC_LONG length = (CC_LONG)strlen(cString);
    // 容器
    unsigned char bytes[CC_SHA1_DIGEST_LENGTH];
    // 3.SHA-1转换
    CC_SHA1(cString, length, bytes);
    NSMutableString *finalstring = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH];
    for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
        [finalstring appendFormat:@"%02x", bytes[i]];
    }
    return finalstring;
}

@end
